home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libtext / fbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  2.1 KB  |  87 lines

  1. /*
  2.  * fbm.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * fbm.c,v 4.1 1994/08/09 08:02:31 explorer Exp
  17.  *
  18.  * fbm.c,v
  19.  * Revision 4.1  1994/08/09  08:02:31  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:14  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:42:06  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "texture.h"
  30. #include "fbm.h"
  31.  
  32. FBm *
  33. FBmCreate(offset, scale, h, lambda, octaves, thresh, mapname)
  34. Float h, lambda, scale, offset, thresh;
  35. int octaves;
  36. char *mapname;
  37. {
  38.     FBm *fbm;
  39.  
  40.     fbm = (FBm *)Malloc(sizeof(FBm));
  41.  
  42.     fbm->beta = 1. + 2*h;
  43.     fbm->omega = pow(lambda, -0.5*fbm->beta);
  44.     fbm->lambda = lambda;
  45.     fbm->scale = scale;
  46.     fbm->offset = offset;
  47.     fbm->thresh = thresh;
  48.     fbm->octaves = octaves;
  49.     if (mapname != (char *)NULL)
  50.         fbm->colormap = ColormapRead(mapname);
  51.     else
  52.         fbm->colormap = (Color *)NULL;
  53.     return fbm;
  54. }
  55.  
  56. void
  57. FBmApply(fbm, prim, ray, pos, norm, gnorm, surf)
  58. FBm *fbm;
  59. Geom *prim;
  60. Ray *ray;
  61. Vector *pos, *norm, *gnorm;
  62. Surface *surf;
  63. {
  64.     Float val;
  65.     int index;
  66.  
  67.     val = fBm(pos, fbm->omega, fbm->lambda, fbm->octaves);
  68.     if (val < fbm->thresh)
  69.         val = fbm->offset;
  70.     else
  71.         val = fbm->offset + fbm->scale*(val - fbm->thresh);
  72.     if (fbm->colormap) {
  73.         index = 255. * val;
  74.         if (index > 255) index = 255;
  75.         if (index < 0) index = 0;
  76.         surf->diff.r *= fbm->colormap[index].r;
  77.         surf->diff.g *= fbm->colormap[index].g;
  78.         surf->diff.b *= fbm->colormap[index].b;
  79.         surf->amb.r *= fbm->colormap[index].r;
  80.         surf->amb.g *= fbm->colormap[index].g;
  81.         surf->amb.b *= fbm->colormap[index].b;
  82.     } else {
  83.         ColorScale(val, surf->diff, &surf->diff);
  84.         ColorScale(val, surf->amb, &surf->amb);
  85.     }
  86. }
  87.